import TabPoint from "@/components/TabPoint";
import TopOperationButton from "@/views/edge/CommonComponents/TopOperationButton";
// 本地数据
import {
    pointList,
    outputTypeOption,
    mapTypeOption,
    keyValueOption,
    mapList,
} from "@/views/edge/inputSourcePage/js/localData";
import KeyValue from "@/views/edge/inputSourcePage/components/KeyValue.vue";
import { mapGetters } from "vuex";
import {
    getSourceMapList,
    setSourceMapList,
    getMapKeyValobj,
    setCheck,
    valiType,
} from "@/views/edge/inputSourcePage/js/index";
import _ from "lodash";
export const optionForm = {
    components: { TabPoint, KeyValue, TopOperationButton },
    computed: {
        ...mapGetters(["metadata", "succSin"]),
        validatorForm() {
            return function (rule, value, callback, item) {
                // 如果没选中,直接返回
                if (item.isCheck === false) {
                    callback();
                }
                const msg = valiType(value, item.type);
                if (msg.length === 0) {
                    callback();
                } else {
                    callback(new Error(msg));
                }
            };
        },
        // 自定义校验自定义属性的参数
        validator() {
            return function (rule, value, callback) {
                var reg = /^[a-zA-Z][-_a-zA-Z0-9]{0,5}/;
                if (reg.test(value)) {
                    callback();
                } else {
                    callback(
                        new Error("属性名称首位必须为字母且不能包含特殊符号和空格!!")
                    );
                }
            };
        },
    },
    data() {
        return {
            mapList: mapList,

            newData: {
                connectedElementName: "",
                type: "",
                options: [],
                annotationType: "SOURCE",
                map: {
                    options: [],
                    payloadOrAttribute: {},
                    type: "",
                },
            },
            form: {
                valueOption: [],
                indexOption: [],
                attTextarea: "",
                isKeyAtt: "",
                mapping: "",
                partition: false,
                customOption: [],
                customOptionMAP: [],
                options: [],
                sourceList: [],
                mapList: [],
                attributeList: [],
                type: "",
                isCorresponding: false,
                name: ''
            },
            pointList: [{
                name: "选项",
                id: "option",
                val: "option",
            },
            {
                name: "数据库",
                id: "shujuku",
                val: "shujuku",
            },
            {
                name: "注释",
                id: "zhushi",
                val: "zhushi",
            }],
            outputTypeOption: outputTypeOption,
            mapTypeOption: mapTypeOption,
            keyValueOption: keyValueOption,
            sources: [],
        };
    },
    mounted() { },
    methods: {
        handClose() {
            this.$emit("handClose");
        },
        submit() {
            // const data = this.sourceList
            // 选项+自定义选项
            this.$refs["formName"].validate((valid) => {
                if (valid) {
                    const sourceOption = getSourceMapList(this.form.sourceList);
                    const customOption = getSourceMapList(this.form.customOption);
                    // map选项+自定义选项
                    const mapOption = getSourceMapList(this.form.mapList);
                    const customOptionMAP = getSourceMapList(this.form.customOptionMAP);
                    this.newData.options = _.concat(sourceOption, customOption);
                    if (!this.newData.map) {
                        this.newData.map = {};
                    }

                    this.newData.map.type = this.form.mapping;
                    this.newData.type = this.form.type;
                    this.newData.map.options = _.concat(mapOption, customOptionMAP);
                    this.newData.annotationType = "SOURCE";
                    this.newData.name = this.form.type;
                    let returnData = {
                        display: false,
                        type: "sourceList",
                        objectState: this.nodeDataRight.objectState,
                        nodeData: this.newData,
                    };
                    this.$emit("returnSource", returnData);
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
        rdbmsChange(val) {
            this.form.sourceList = _.cloneDeep(
                this.sources.find((item) => item.name == "rdbms")?.parameters2[val]
                    .parameters || []
            );
        },
        changeType(name) {
            // 单独处理rdbms
            if (name == "rdbms") {
                this.form.sourceList = _.cloneDeep(
                    this.sources.find((item) => item.name == name)?.parameters2[0]
                        .parameters || []
                );
                console.log(this.form.sourceList);
                // this.form.sourceList.sort((a, b) => a.optional - b.optional);
            } else {
                this.form.sourceList = _.cloneDeep(
                    this.sources.find((item) => item.name == name)?.parameters || []
                );
                this.form.sourceList.sort((a, b) => a.optional - b.optional);
            }

            this.form.type = name;
        },
        handAddOption(key) {
            this.form[key].push({
                name: "",
                defaultValue: "",
                isCheck: true,
            });
        },
        handDel(index, key) {
            this.form[key].splice(index, 1);
        },
    },
};